package org.yn.finance.controller;

import org.yn.finance.service.IDepositService;
import org.yn.finance.domain.Deposit;
import org.yn.finance.query.DepositQuery;
import org.yn.base.util.AjaxResult;
import org.yn.base.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.yn.auth.annotation.PermissionAnnotation;
import org.yn.finance.vo.DepositSalesVO;

import java.util.List;

@PermissionAnnotation(name="deposit管理",descs = "deposit管理")
@Api("deposit管理接口")
@RestController
@RequestMapping("/deposit")
public class DepositController {
    @Autowired
    public IDepositService depositService;


    /**
     * 保存和修改公用的
     * @param deposit  传递的实体
     * @return Ajaxresult转换结果
     */
    @PermissionAnnotation(name="保存和修改Deposit",descs = "保存和修改Deposit")
    @ApiOperation(value = "保存和修改")
    @PostMapping
    public AjaxResult updateOrInsert(@RequestBody Deposit deposit){
        try {
            if( deposit.getId()!=null)
                depositService.update(deposit);
            else
                depositService.insert(deposit);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @PermissionAnnotation(name="删除Deposit",descs = "删除Deposit")
    @ApiOperation(value = "删除")
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            depositService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @PermissionAnnotation(name="获取单个Deposit",descs = "获取单个Deposit")
    @ApiOperation(value = "获取单个Deposit")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id)
    {
        try {
            Deposit deposit = depositService.selectById(id);
            return AjaxResult.success(deposit);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的XX信息
    * @return
    */
    @PermissionAnnotation(name="获取所有Deposit",descs = "获取所有Deposit")
    @ApiOperation(value = "获取所有Deposit")
    @GetMapping
    public AjaxResult selectAll(){

        try {
            List< Deposit> list = depositService.selectAll();
            return AjaxResult.success(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageInfo 分页对象
    */
    @PermissionAnnotation(name="分页查询Deposit",descs = "分页查询Deposit")
    @ApiOperation(value = "分页查询Deposit")
    @PutMapping
    public AjaxResult queryPage(@RequestBody DepositQuery query)
    {
        try {
            PageInfo<Deposit> pageInfo = depositService.queryPage(query);
            return AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("获取分页数据失败！"+e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PermissionAnnotation(name="批量删除Deposit",descs = "批量删除Deposit")
    @ApiOperation(value = "批量删除Deposit")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
                depositService.batchDelete(ids);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }
    //统计定金金额，和定金笔数
    @GetMapping("/getDepositSales")
    public AjaxResult getDepositSales(){
        try {
          List<DepositSalesVO> depositSalesVOList=  depositService.getDepositSales();
            return AjaxResult.success(depositSalesVOList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("批量删除失败"+e.getMessage());
        }
    }


}
